package com.bj58.wuxian.hystrix.failback;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixThreadPoolProperties;
import com.netflix.hystrix.HystrixCommand.Setter;
import com.netflix.hystrix.exception.HystrixBadRequestException;
import com.netflix.hystrix.exception.HystrixTimeoutException;

/**
 * 
 * 以下四种情况将触发getFallback调用：
 * 1）run()方法抛出非HystrixBadRequestException异常
 * 2）run()方法调用超时
 * 3）熔断器开启拦截调用
 * 4）线程池/队列/信号量是否跑满
 * 
 * 实现getFallback()后，执行命令时遇到以上4种情况将被fallback接管，不会抛出异常或其他
 */
public class HystrixFallback4Exception extends HystrixCommand<String> {

    private final String name;

    public HystrixFallback4Exception(String name) {
    	super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("failBackGroup"))
				.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(2))
				.andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(1000)));
        this.name = name;
    }

    @Override
    protected String run() throws Exception {
    	
    	/*---------------会触发fallback的case-------------------*/
    	// 超时
	//	TimeUnit.MILLISECONDS.sleep(1200);
    	
    	// 除零异常
     //	int i = 1/0;
    	
    	// 主动抛出异常
//       throw new HystrixTimeoutException();
//        throw new RuntimeException("this command will trigger fallback");
//        throw new Exception("this command will trigger fallback");
//    	throw new HystrixRuntimeException(FailureType.BAD_REQUEST_EXCEPTION, commandClass, message, cause, fallbackException);
        
    	/*---------------不会触发fallback的case-------------------*/
    	// 被捕获的异常不会触发fallback
//    	try {
//    		throw new RuntimeException("this command never trigger fallback");
//    	} catch(Exception e) {
//    		e.printStackTrace();
//    	}
        
    	// HystrixBadRequestException异常由非法参数或非系统错误引起，不会触发fallback，也不会被计入熔断器
      //  throw new HystrixBadRequestException("HystrixBadRequestException is never trigger fallback");
        
		return name;
    }

    @Override
    protected String getFallback() {
        return "fallback: " + name;
    }

}
